package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.entity.Video;
import com.atguigu.guli.service.edu.mapper.ChapterMapper;
import com.atguigu.guli.service.edu.service.ChapterService;
import com.atguigu.guli.service.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-07-26
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    VideoService videoService;

    @Override
    public List<Chapter> getChaptersByCourseId(String id) {

        //1.先获取到该课程下的所有章
        LambdaQueryWrapper<Chapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Chapter::getSort); //按sort升序排列
        queryWrapper.eq(Chapter::getCourseId,id);
        List<Chapter> chapters = baseMapper.selectList(queryWrapper);

        //2.再获取到每章下的课时小节
        for (Chapter chapter : chapters) {
            LambdaQueryWrapper<Video> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.orderByAsc(Video::getSort); //按sort升序排列
            queryWrapper2.eq(Video::getChapterId,chapter.getId());
            List<Video> videos = videoService.list(queryWrapper2);
            chapter.setVideos(videos);
        }

        return chapters;
    }
}
